Ext.onReady(function() {
	/*******未处理按揭开始*********/
	var unDealSearchForm = Ext.create("Ext.form.FormPanel", {
		collapsible: true,
		height: 200,
	    fieldDefaults: {
	        labelWidth: 80,
            labelAlign: "right",
            margin: "5 0 0 0"
	    },
        items: [
        	//权限资源隐藏字段
			{xtype:"textfield", name:"resourcesCode", value: resourcesCode, hidden: true},
			{xtype:"textfield", name:"resourcesName", value: "resName_mort_list", hidden: true},
        	{
	        	xtype : 'textfield', fieldLabel : '处理状态',
		    	name: "processStatus", readOnly: true, hidden: true, value: 0
            },{
	        	xtype : 'textfield', fieldLabel : '树节点',
		    	name: "treeId", readOnly: true, hidden: true
            },{
	        	xtype: "fieldcontainer",
	        	layout: "hbox",
	        	items: [{
	        		xtype: "textfield",
			        fieldLabel: "业务编号",
			        name: 'signCode'
		    	},{
			    	xtype: "textfield",
			        fieldLabel: "客户名称",
			        name: 'customerName'
			    }]
	    	},{
	        	xtype: "fieldcontainer",
	        	layout: "hbox",
	        	items: [{
	        		xtype: "datefield",
			        fieldLabel: "签约开始日期",
			        name: "ext_signedDateBefore",
			        format: "Y-m-d"
			    },{
			    	xtype: "datefield",
			        fieldLabel: "签约结束日期",
			        name: 'ext_signedDateAfter',
			        format: "Y-m-d"
			    }]
	    	},{
	        	xtype: "checkboxgroup",
	        	id:"mortgageReceivedStatus",
	        	name:"mortgageReceivedStatus",
	        	fieldLabel: "按揭状态",
	        	width: 400,
	        	items: [{
	        		boxLabel: "未收款",
	        		inputValue:'0'
			    },{
			    	boxLabel: "部分收款",
	        		inputValue:'1'
			    },{
			    	boxLabel: "全部收款",
	        		inputValue:'2'
			    }]
	    	},{
	        	xtype: "checkboxgroup",
	        	id:"mortgageCheckStatus",
	        	name:"mortgageCheckStatus",
	        	fieldLabel: "资金复核状态",
	        	width: 400,
	        	items: [{
	        		boxLabel: "未复核",
	        		inputValue:'0'
			    },{
			    	boxLabel: "复核通过",
	        		inputValue:'1'
			    },{
			    	boxLabel: "复核未通过",
	        		inputValue:'2'
			    }]
    		}
    	],
	    buttons: [{
	    		xtype: "button", text: "查询", 
  	        	handler:function(){
  	        		//获取通过checkboxgroup定义的checkbox值
  	        		//审核状态
  	        		var mortgageReceivedStatus = Ext.getCmp('mortgageReceivedStatus').getChecked();
  	        		var mortgageReceivedStatusChked="";
  	        		Ext.Array.each(mortgageReceivedStatus, function(item){
  	        			if(mortgageReceivedStatusChked){
  	        				mortgageReceivedStatusChked += ",";
  	        			}
  	        			mortgageReceivedStatusChked += item.inputValue;
  	        		});
  	        		unDealGridStore.getProxy().extraParams.mortgageReceivedStatus = mortgageReceivedStatusChked;
  	        		//业务状态
  	        		var mortgageCheckStatus = Ext.getCmp('mortgageCheckStatus').getChecked();
  	        		var mortgageCheckStatusChked="";
  	        		Ext.Array.each(mortgageCheckStatus, function(item){
  	        			if(mortgageCheckStatusChked){
  	        				mortgageCheckStatusChked += ",";
  	        			}
  	        			mortgageCheckStatusChked += item.inputValue;
  	        		});
  	        		unDealGridStore.getProxy().extraParams.mortgageCheckStatus = mortgageCheckStatusChked;
  	        		unDealGridStore.load();
  	        		var pagingTool=Ext.getCmp("pagingTool");
  	        		pagingTool.moveFirst();//跳至第一页
  	        	}
  	        },
  	        {
	  			xtype: "button",
	  	        text: "清空",
	  	        handler:function(){
	        		unDealSearchForm.getForm().reset();
	        	}
  	        }
  	    ] 
	});
	
	var unDealGridStore = Ext.create('Ext.data.Store', {
        fields: [
        		"id","paymentMethod", "roomName", "customerName", "signedDate",
        		"signAmount", "hrrReceived", "proportionValue", "housingFundType",
        		"mortgageAmount", "signedId", "fAmount","expectLoadDate"
				],
        pageSize: 10,  //页容量5条数据
        //是否在服务端排序 （true的话，在客户端就不能排序）
        remoteSort: false,
        proxy: {
            type: 'ajax',
            url: jypath+'/deal/mortgage/mortgageList',
            actionMethods: { read: 'POST' },
            reader: {
            	//这里的reader为数据存储组织的地方，下面的配置是为json格式的数据，例如：[{"total":50,"rows":[{"a":"3","b":"4"}]}]
                type: 'json', //返回数据类型为json格式
                root: 'rows',  //数据
                totalProperty: 'total' //数据总条数
            }
        },
        autoLoad: true  //即时加载数据
    });
    
    unDealGridStore.on("beforeload",function(){
        Ext.apply(unDealGridStore.proxy.extraParams, unDealSearchForm.getForm().getValues());
    });
    
	var unDealColumns = [{  
        text : "操作",  
        dataIndex : 'actioncolumn',
        locked   : true,
        renderer : function(value,cellmeta,record,rowIndex,columnIndex,store) {
        	var rec = unDealGrid.getStore().getAt(rowIndex);
        	var url = jypath + "/deal/mortgage/mortgageDetail?id=" + rec.get('id')+"&changeType=true";
        	if(dealMortgageAdd==false){
        		var html = '<a href="' + url + '">按揭处理</a>';
        	}
        	
        	return html;
        }
        },
    	{text: '付款方式',  dataIndex: 'paymentMethod'},
        {text: '房号', dataIndex: 'roomName',
            renderer: function(value,cellmeta,record,rowIndex,columnIndex,store) {
            	
           	 var rec = unDealGrid.getStore().getAt(rowIndex);
             if(rec.get('signedId')!=null){
             	 return "<a href='"+jypath+"/customer/signed/toView?id="+rec.get('signedId')+"&returnUrl=,deal,mortgage,index' style='cursor:pointer'>"+value+"</a>"
             }
          	 return value;
          }
    	},
        {text: '客户名称', dataIndex: 'customerName'},
        {text: '签约日期', dataIndex: 'signedDate',format: 'Y-m-d',
	    	renderer : function(val) {
				if(val!=null&&val!=""){
					return Ext.Date.format(new Date(val), "Y-m-d");
				}
				return null;
			}
		},
        {text: '合同金额', dataIndex: 'signAmount', renderer: Ext.util.Format.numberRenderer('0,000.00')},
        {text: '合同已收金额', dataIndex: 'hrrReceived', renderer: Ext.util.Format.numberRenderer('0,000.00')},
        {text: '已收金额占比', dataIndex: 'proportionValue',
       		renderer : function(val) {
				if(val!=null&&val!=""){
					return val+"%";
				}
				return "0.00%";
			}
		},
        {text: '房款类型', dataIndex: 'housingFundType'},
        {text: '按揭金额', dataIndex: 'mortgageAmount', renderer: Ext.util.Format.numberRenderer('0,000.00')},
        {text: '按揭已放款金额', dataIndex: 'fAmount', renderer: Ext.util.Format.numberRenderer('0,000.00')},
        {text: '贷款审核进度', dataIndex: 'progressLoan'},
        {text: '预计放贷时间', dataIndex: 'expectLoadDate',
        	renderer : function(val) {
				if(val!=null&&val!=""){
					return Ext.Date.format(new Date(val), "Y-m-d");
				}
				return null;
			}
        }/*,
        {  
            text : "操作",  
            dataIndex : 'actioncolumn',
            renderer : function(value,cellmeta,record,rowIndex,columnIndex,store) {
            	var rec = unDealGrid.getStore().getAt(rowIndex);
	        	var url = jypath + "/deal/mortgage/mortgageDetail?id=" + rec.get('id')+"&changeType=true";
	        	if(dealMortgageAdd==false){
	        		var html = '<a href="' + url + '">按揭处理</a>';
	        	}
	        	
	        	return html;
            }
        }*/
    ]
    
	var unDealGrid = Ext.create('Ext.grid.Panel', {
        columnLines: true,
        minHeight: 400,
	    store: unDealGridStore,
	    columns: unDealColumns,
	    dockedItems: [{
            xtype: 'pagingtoolbar',
            store: unDealGridStore,
            dock: 'bottom',
            emptyMsg: '没有数据',
            displayInfo: true,
            displayMsg: '当前显示{0}-{1}条记录 / 共{2}条记录 ',
            beforePageText: '第',
            afterPageText: '页/共{0}页',
            id:'pagingTool'
        }]
	});
	
	/*******未处理按揭结束*********/
	
	/*******已处理按揭开始*********/
	//审批进度
	var progressLoanList = [
         {"value":"0","text":"未办理"},
         {"value":"1","text":"未达办理条件"},
         {"value":"2","text":"资料/手续齐全"},
         {"value":"3","text":"抵押办理中"},
         {"value":"4","text":"贷款审批中"},
         {"value":"5","text":"复审需补材料"},
         {"value":"6","text":"待放款(无额度)"},
         {"value":"7","text":"待放款(有额度)"},
         {"value":"8","text":"已放款"}
     ];
	var progressLoanStore = Ext.data.Store({
    	autoDestroy: true,
        fields :["value","text"],
        data : progressLoanList
    });

	var bankStore = Ext.data.Store({
            singleton:true,
            autoDestroy:true,
            // fields: ["id", "buildingName"],
            proxy:{
                type:'ajax',
                actionMethods: {
                    create : 'POST',
                    read   : 'POST', // by default GET
                    update : 'POST',
                    destroy: 'POST'
                },
                url:contextPath+'/backstage/sysDict/find',
				extraParams:{dictionaryType:'BK'},
                reader:{
                    type: 'json',
                    root: 'root'  //数据
                }
            }
	});
	
	//是否逾期未放款
	var selOverdueLendersList = [
         {"value":"0","text":"全部"},
         {"value":"1","text":"是"},
         {"value":"2","text":"否"}
     ];
	var selOverdueLendersStore = Ext.data.Store({
    	autoDestroy: true,
        fields :["value","text"],
        data : selOverdueLendersList
    });
	
	var dealSearchForm = Ext.create("Ext.form.FormPanel", {
		collapsible: true,
		defaultType: 'textfield',
		fieldDefaults: {
	        labelWidth: 120,
            labelAlign: "right",
            margin: "5 0 0 0"
	    },
	    items: [
	    	//权限资源隐藏字段
			{xtype:"textfield", name:"resourcesCode", value: resourcesCode, hidden: true},
			{xtype:"textfield", name:"resourcesName", value: "resName_mort_list", hidden: true},
	    	{
	        	xtype : 'textfield', fieldLabel : '处理状态',
		    	name: "processStatus", readOnly: true, hidden: true, value: 1
            },{
	        	xtype : 'textfield', fieldLabel : '树节点',
		    	name: "treeId", readOnly: true, hidden: true
            },{
	        	xtype: "fieldcontainer",
	        	layout: "hbox",
	        	items: [{
	        		xtype: "textfield",
			        fieldLabel: "业务编号",
			        name: 'signCode'
		    	},{
			    	xtype: "textfield",
			        fieldLabel: "客户名称",
			        name: 'customerName'
			    }]
	    	},{
	        	xtype: "fieldcontainer",
	        	layout: "hbox",
	        	items: [{
	        		xtype: "datefield",
			        fieldLabel: "签约开始日期",
			        name: "ext_signedDateBefore",
			        format: "Y-m-d"
			    },{
			    	xtype: "datefield",
			        fieldLabel: "签约结束日期",
			        name: 'ext_signedDateAfter',
			        format: "Y-m-d"
			    }]
	    	},{
	        	xtype: "fieldcontainer",
	        	layout: "hbox",
	        	items: [{
	        		xtype : 'combobox',
					fieldLabel : '审批进度',
					store: progressLoanStore,
			    	displayField: "text",
			    	valueField: "value",
			    	id: "progressLoan",
			    	name: "progressLoan",
			    	editable: false
			    },{
			    	// xtype: "textfield",
			    	xtype: "combobox",
			        fieldLabel: "按揭银行",
					displayField:"text",
					store:bankStore,
					valueField:"value",
			        name: 'bankDetailName',
					editable:false
			    }]
	    	},{
	        	xtype: "fieldcontainer",
	        	layout: "hbox",
	        	items: [{
	        		xtype: "datefield",
			        fieldLabel: "处理开始日期",
			        name: "ext_statementDateBefore",
			        format: "Y-m-d"
			    },{
			    	xtype: "datefield",
			        fieldLabel: "处理结束日期",
			        name: 'ext_statementDateAfter',
			        format: "Y-m-d"
			    }]
	    	},{
	        	xtype: "checkboxgroup",
	        	id:"businessStatus",
	        	name:"businessStatus",
	        	fieldLabel: "签约状态",
	        	width: 400,
	        	items: [{
	        		boxLabel: "签约",
	        		inputValue:'1'
			    },{
			    	boxLabel: "已退房",
	        		inputValue:'2'
			    },{
			    	boxLabel: "已换房",
	        		inputValue:'3'
			    },{
			    	boxLabel: "已更名",
	        		inputValue:'4'
			    }]
	    	},{
	        	xtype: "checkboxgroup",
	        	id:"paymentMethod",
	        	name:"paymentMethod",
	        	fieldLabel: "付款方式",
	        	width: 400,
	        	items: [{
	        		boxLabel: "商业贷款",
	        		inputValue:'3'
			    },{
			    	boxLabel: "公积金贷款",
	        		inputValue:'4'
			    },{
			    	boxLabel: "组合贷款",
	        		inputValue:'5'
			    }]
	    	},{
	    		xtype:'combobox',  
                id:'selOverdueLenders',  
                store: selOverdueLendersStore,
                displayField: "text",           
                valueField: "value",  
                fieldLabel:'是否逾期未放款',  
                typeAhead: true,
    			name:'selOverdueLenders'
    		}
	    ],
	    buttons: [{
	    		xtype: "button", text: "查询", buttonAlign: "left",
  	        	handler:function(){
  	        		//获取通过checkboxgroup定义的checkbox值
  	        		//签约状态
  	        		var businessStatus = Ext.getCmp('businessStatus').getChecked();
  	        		var businessStatusChked="";
  	        		Ext.Array.each(businessStatus, function(item){
  	        			if(businessStatusChked){
  	        				businessStatusChked += ",";
  	        			}
  	        			businessStatusChked += item.inputValue;
  	        		});
  	        		dealGridStore.getProxy().extraParams.businessStatus = businessStatusChked;
  	        		//业务状态
  	        		var paymentMethod = Ext.getCmp('paymentMethod').getChecked();
  	        		var paymentMethodChked="";
  	        		Ext.Array.each(paymentMethod, function(item){
  	        			if(paymentMethodChked){
  	        				paymentMethodChked += ",";
  	        			}
  	        			paymentMethodChked += item.inputValue;
  	        		});
  	        		dealGridStore.getProxy().extraParams.paymentMethod = paymentMethodChked;
  	        		dealGridStore.load();
  	        		var pagingTools=Ext.getCmp("pagingTools");
  	        		pagingTools.moveFirst();//跳至第一页
  	        	}
  	        },
  	        {
	  			xtype: "button",
	  	        text: "清空",
	  	        handler:function(){
	        		dealSearchForm.getForm().reset();
	        	}
  	        }
  	    ]
	});
	
	var dealGridStore = Ext.create('Ext.data.Store', {
        fields: [
        		"id","isOverdueLenders","overdueDayNum","contactType","paymentMethod", "roomName", "customerName", "signedDate",
        		"signAmount", "hrrReceived", "proportionValue", "housingFundType",
        		"mortgageAmount","progressLoan","expectLoadDate","bankName","bankDetailName",
        		"statementDate","hrrReceived","updateTime"
				],
        pageSize: 10,  //页容量5条数据
        //是否在服务端排序 （true的话，在客户端就不能排序）
        remoteSort: false,
        proxy: {
            type: 'ajax',
            url: jypath+'/deal/mortgage/mortgageList',
            //默认参数,必填.
            actionMethods: { read: 'POST' },
            reader: {
            	//这里的reader为数据存储组织的地方，下面的配置是为json格式的数据，例如：[{"total":50,"rows":[{"a":"3","b":"4"}]}]
                type: 'json', //返回数据类型为json格式
                root: 'rows',  //数据
                totalProperty: 'total' //数据总条数
            }
        },
        autoLoad: true  //即时加载数据
    });
    
    dealGridStore.on("beforeload",function(){
        Ext.apply(dealGridStore.proxy.extraParams, dealSearchForm.getForm().getValues());
    });
    
    var dealColumns = [
		{  
		    text : "查看",
		    dataIndex : 'actioncolumn',
		    renderer : function(value,cellmeta,record,rowIndex,columnIndex,store) {
		    	var rec = dealGrid.getStore().getAt(rowIndex);
		    	var url = jypath + "/deal/mortgage/mortgageDetail?id=" + rec.get('id');
		    	var html = '<a href="' + url + '">查看/修改</a>';
		    	return html;
		    }
		},
		{text: '是否逾期未放款',  dataIndex: 'isOverdueLenders'},
	    {text: '逾期天数',  dataIndex: 'overdueDayNum'},
    	{text: '风险分类',  dataIndex: 'contactType'},
        {text: '付款方式', dataIndex: 'paymentMethod'},
        {text: '房号', dataIndex: 'roomName'},
        {text: '客户名称', dataIndex: 'customerName'},
        {text: '签约日期', dataIndex: 'signedDate',
        	renderer : function(val) {
				if(val!=null&&val!=""){
					return Ext.Date.format(new Date(val), "Y-m-d");
				}
				return null;
			}
        },
        {text: '合同金额', dataIndex: 'signAmount', renderer: Ext.util.Format.numberRenderer('0,000.00')},
        {text: '合同已收金额', dataIndex: 'hrrReceived', renderer: Ext.util.Format.numberRenderer('0,000.00')},
        {text: '已收金额占比', dataIndex: 'proportionValue',
        	renderer : function(val) {
				if(val!=null&&val!=""){
					return val+"%";
				}
				return "0.00%";
			}
        },
        {text: '房款类型', dataIndex: 'housingFundType'},
        {text: '按揭金额', dataIndex: 'mortgageAmount', renderer: Ext.util.Format.numberRenderer('0,000.00')},
        {text: '贷款审核进度', dataIndex: 'progressLoan'},
        {text: '预计放贷时间', dataIndex: 'expectLoadDate',
        	renderer : function(val) {
				if(val!=null&&val!=""){
					return Ext.Date.format(new Date(val), "Y-m-d");
				}
				return null;
			}
        },
        {text: '贷款银行', dataIndex: 'bankName'},
        {text: '具体按揭银行名称', dataIndex: 'bankDetailName'},
        {text: '放贷日期', dataIndex: 'statementDate',
        	renderer : function(val) {
				if(val!=null&&val!=""){
					return Ext.Date.format(new Date(val), "Y-m-d");
				}
				return null;
			}
        },
        {text: '放贷金额', dataIndex: 'hrrReceived', renderer: Ext.util.Format.numberRenderer('0,000.00')},
        {text: '处理时间', dataIndex: 'updateTime',
        	renderer : function(val) {
				if(val!=null&&val!=""){
					return Ext.Date.format(new Date(val), "Y-m-d");
				}
				return null;
			}
        },
        {  
            text : "操作",
            dataIndex : 'actioncolumn',
            renderer : function(value,cellmeta,record,rowIndex,columnIndex,store) {
            	var rec = dealGrid.getStore().getAt(rowIndex);
	        	var url = jypath + "/deal/mortgage/mortgageDetail?id=" + rec.get('id');
	        	var html = '<a href="' + url + '">查看/修改</a>';
	        	return html;
            }
        }
    ]
    
	var dealGrid = Ext.create('Ext.grid.Panel', {
	    store: dealGridStore,
	    columnLines: true,
        minHeight: 350,
		viewConfig : {
	 		forceFit : true
	 	},
	    columns: dealColumns,
	    dockedItems: [{
            xtype: 'pagingtoolbar',
            store: dealGridStore,
            dock: 'bottom',
            emptyMsg: '没有数据',
            displayInfo: true,
            displayMsg: '当前显示{0}-{1}条记录 / 共{2}条记录 ',
            beforePageText: '第',
            afterPageText: '页/共{0}页',
            id:'pagingTools'
        }]
	});
	/*******已处理按揭结束*********/

	/**********按揭Tab开始***********/
	var mortgageTab = Ext.create("Ext.tab.Panel", {
		region: 'center',
		layout : 'fit',
        defaults :{
            autoScroll: true
        },
	    items: [{
	    	xtype: "form",
	    	id: "waitTab",
	        title: '按揭待处理',
	        items: [unDealSearchForm, unDealGrid]
	    }, {
	    	xtype: "form",
	    	id: "didTab",
	        title: '按揭已处理',
	        items: [dealSearchForm, dealGrid]
	    }],
	    listeners: {
	    	click: {
	    		element: "el",
	    		fn: function(){
	    			//console.log(mortgageTab.getActiveTab().getId());
	    		}
	    	}
	    }
	});
	/**********按揭Tab结束***********/
	
	/**********按揭管理右侧查询及Tab开始***********/
	var rightContent = Ext.create("Ext.form.Panel", {
		region: 'center',
		items: [mortgageTab]
	});
	/**********按揭管理右侧查询及Tab开始***********/
	
	/***********按揭管理左侧菜单树开始**********/
	var treeStoreJson = null;
	$.ajax({
		type : "POST",
		url : jypath + "/deal/mortgage/treeNodeList?resourcesCode=menu_0003_0001&resourcesName=resName_pro_list",
		dataType : "json",
		async : false,
		success : function(data) {
			treeStoreJson = data;
		}
	});
	var treeStore = Ext.create('Ext.data.TreeStore', {
		root : {
			expanded : false,
			children : treeStoreJson
		}
	});
	
	var treeSelected = function(node, event) {
		var treeId = event.data.id;
		unDealSearchForm.getForm().findField("treeId").setValue(treeId);
		dealSearchForm.getForm().findField("treeId").setValue(treeId);
		//选中的标签
		var activeTabId = mortgageTab.getActiveTab().getId();
		//判断Tab选中的对象,决定刷新哪一个GridStroe
		if(activeTabId == "waitTab"){
			unDealGridStore.load();
		}else if(activeTabId == "didTab"){
			dealGridStore.load();
		}
	}
	
	var leftTree = Ext.create("Ext.tree.Panel", {
		title: '项目树',
		region: 'west',
    	width: 220,
	    store: treeStore,
	    expand: false,
	    rootVisible: false,
	    listeners : {
			itemclick : treeSelected
		}
	});
	
	/***********按揭管理左侧菜单树结束**********/
	
	/***********按揭管理ViewPort开始**********/
	var viewPort = Ext.create("Ext.container.Viewport", {
		layout: "border",
		renderTo: Ext.getBody(),
		items: [leftTree, mortgageTab]
	});
	/***********按揭管理ViewPort结束**********/
});